home *** CD-ROM | disk | FTP | other *** search
- /* vector-matrix.c */
- /* written by : Jason R. Wilson 2/21/93 */
-
- /* this package provides basic vector and matrix manipulations */
-
- #include <math.h>
- #include <stdio.h>
- #include "datastruct.h"
-
- /*****************************************************************************/
- /* Function: Length */
- /* Purpose : To return the length of a 3-D vector. */
- /*****************************************************************************/
- double Length(Vector vec) {
-
- return sqrt(vec.dx*vec.dx + vec.dy*vec.dy + vec.dz*vec.dz);
- }
-
-
- /*****************************************************************************/
- /* Function: Normalize */
- /* Purpose : To return the normalized version of a vector. */
- /*****************************************************************************/
- Vector Normalize(Vector vec) {
-
- Vector temp;
- double len;
-
- len = Length(vec);
- if (len == 0.0)
- printf("zero vector in Normalize");
- else {
- temp.dx = vec.dx/len;
- temp.dy = vec.dy/len;
- temp.dz = vec.dz/len;
- }
- return temp;
- }
-
-
- /*****************************************************************************/
- /* Function: Scale */
- /* Purpose : To scalar multiply the components of a 3-D vector by the */
- /* doubleing point number, scale. */
- /*****************************************************************************/
- Vector Scale(Vector vec,double scale) {
-
- Vector temp;
-
- temp.dx = scale*vec.dx;
- temp.dy = scale*vec.dy;
- temp.dz = scale*vec.dz;
- return temp;
- }
-
- /*****************************************************************************/
- /* Function: Sub */
- /* Purpose : To return the difference of vec1 and vec2 ==> vec1 - vec2. */
- /*****************************************************************************/
- Vector Sub(Vector vec1, Vector vec2) {
-
- Vector temp;
-
- temp.dx = vec1.dx - vec2.dx;
- temp.dy = vec1.dy - vec2.dy;
- temp.dz = vec1.dz - vec2.dz;
- return temp;
- }
-
-
- /*****************************************************************************/
- /* Function: Add */
- /* Purpose : To return the sum of vec1 and vec2 ==> vec1 + vec2. */
- /*****************************************************************************/
- Vector Add(Vector vec1, Vector vec2) {
-
- Vector temp;
-
- temp.dx = vec1.dx + vec2.dx;
- temp.dy = vec1.dy + vec2.dy;
- temp.dz = vec1.dz + vec2.dz;
- return temp;
- }
-
-
- /*****************************************************************************/
- /* Function: Dotprod */
- /* Purpose : To return the dot product of vectors, vec1 and vec2. */
- /*****************************************************************************/
- double Dotprod(Vector vec1, Vector vec2) {
-
- return (vec1.dx*vec2.dx + vec1.dy*vec2.dy + vec1.dz*vec2.dz);
- }
-
-
- /*****************************************************************************/
- /* Function: Cross */
- /* Purpose : To return the cross product of vectors, vec1 and vec2. */
- /*****************************************************************************/
- Vector Cross(Vector vec1, Vector vec2) {
-
- Vector temp;
-
- temp.dx = vec1.dy*vec2.dz - vec1.dz*vec2.dy;
- temp.dy = vec1.dz*vec2.dx - vec1.dx*vec2.dz;
- temp.dz = vec1.dx*vec2.dy - vec1.dy*vec2.dx;
- return temp;
- }
-
- /***********************************************************************/
- /* Function: Transpose */
- /* this routine puts the transpose of matrix a into matrix b */
- /***********************************************************************/
- void Transpose (Matrix *a,Matrix *b) {
-
- b->mat[0] = a->mat[0];
- b->mat[1] = a->mat[4];
- b->mat[2] = a->mat[8];
- b->mat[3] = a->mat[12];
- b->mat[4] = a->mat[1];
- b->mat[5] = a->mat[5];
- b->mat[6] = a->mat[9];
- b->mat[7] = a->mat[13];
- b->mat[8] = a->mat[2];
- b->mat[9] = a->mat[6];
- b->mat[10] = a->mat[10];
- b->mat[11] = a->mat[14];
- b->mat[12] = a->mat[3];
- b->mat[13] = a->mat[7];
- b->mat[14] = a->mat[11];
- b->mat[15] = a->mat[15];
-
- }
- /*****************************************************************************/
- /* Function: pointMultiply */
- /* Purpose : To vector multiply the n by n matrix, b, by the n-element */
- /* vector, a, to produce the n-element vector c. */
- /*****************************************************************************/
- void pointMultiply(Point a, double *b, double *c) {
-
- c[0] = a.x * b[0] + a.y * b[4] + a.z * b[8] + 1.0 * b[12];
- c[1] = a.x * b[1] + a.y * b[5] + a.z * b[9] + 1.0 * b[13];
- c[2] = a.x * b[2] + a.y * b[6] + a.z * b[10] + 1.0 * b[14];
- c[3] = a.x * b[3] + a.y * b[7] + a.z * b[11] + 1.0 * b[15];
- }
-
- /***********************************************************************/
- void HomogMultiply (double *a,double *b)
- /* multiplies a homog. point by a 4X4 matrix */
- {
- double temp[4];
- temp[0] = a[0] * b[0] + a[1] * b[4] + a[2] * b[8] + a[3] * b[12];
- temp[1] = a[0] * b[1] + a[1] * b[5] + a[2] * b[9] + a[3] * b[13];
- temp[2] = a[0] * b[2] + a[1] * b[6] + a[2] * b[10] + a[3] * b[14];
- temp[3] = a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3] * b[15];
- a[0] = temp[0];
- a[1] = temp[1];
- a[2] = temp[2];
- a[3] = temp[3];
- }
-
- /***********************************************************************/
-
- void matrixMultiply (int n, double *a, double *b, double *c)
- /* Multiplies the nxn matrix a by the nxn matrix b to produce the nxn matrix
- c. */
- {
- int i, j, k, posa, posb, posc;
- double sum;
-
- for (i=0; i<n; i++)
- for (j=0; j<n; j++)
- {
- posc = i*n + j;
- sum = 0.0;
- for (k=0; k<n; k++)
- {
- posa = i*n + k;
- posb = k*n + j;
- sum += a[posa] * b[posb];
- }
- c[posc] = sum;
- }
- }
-
-
- void createIdentity (int n, double *a)
- /* Creats an nxn identity matrix in a. */
- {
- int i, j;
-
- for (i=0; i<n; i++)
- for (j=0; j<n; j++)
- a[i*n + j] = 0.0;
- for (i=0; i<n; i++)
- a[i*n + i] = 1.0;
- }
-
- void matrixCopy (double *a, double *b)
- /* copies matrix a into matrix b (assumes 4X4) */
- {
- int loop;
-
- for (loop = 0;loop < 16;loop++)
- b[loop] = a[loop];
- }
-
- void ProduceTransform (Point VRP,Vector u, Vector v, Vector n,
- double Eye,Matrix *Composition)
-
- /* build the transform matrix that transforms coordinate systems and
- does perspective projects */
-
- {
- Matrix ViewTransform, Perspective;
- Vector rprime, r;
-
- r.dx = VRP.x;
- r.dy = VRP.y;
- r.dz = VRP.z;
-
- rprime.dx = (-1)*Dotprod(r,u);
- rprime.dy = (-1)*Dotprod(r,v);
- rprime.dz = (-1)*Dotprod(r,n);
-
- createIdentity (4,ViewTransform.mat);
-
- ViewTransform.mat[0] = u.dx;
- ViewTransform.mat[1] = v.dx;
- ViewTransform.mat[2] = n.dx;
- ViewTransform.mat[4] = u.dy;
- ViewTransform.mat[5] = v.dy;
- ViewTransform.mat[6] = n.dy;
- ViewTransform.mat[8] = u.dz;
- ViewTransform.mat[9] = v.dz;
- ViewTransform.mat[10] = n.dz;
- ViewTransform.mat[12] = rprime.dx;
- ViewTransform.mat[13] = rprime.dy;
- ViewTransform.mat[14] = rprime.dz;
-
- /* Build the perspective transform matrix */
-
- createIdentity (4,Perspective.mat);
- Perspective.mat[11] = (-1)/Eye;
-
- /* Compose the 2 matrices */
-
- matrixMultiply (4,ViewTransform.mat,Perspective.mat,Composition->mat);
-
- }
-
-
- /*----------------------------------------------------------------------*/
-
- void TransformView (ObjectCell *ObjectHead,Matrix Composition)
- /* This routine transforms every vertex into view coordinates, */
- /* and does perspective transformation */
-
- {
- VertexCell *CurrentVert;
- ObjectCell *CurrentObject;
-
- CurrentObject = ObjectHead;
-
- while (!(CurrentObject == NULL))
- {
- CurrentVert = CurrentObject->VertexHead;
-
- while (!(CurrentVert == NULL))
- {
- pointMultiply (CurrentVert->WorldPosition,Composition.mat,
- CurrentVert->ViewPosition.hom);
- CurrentVert = CurrentVert->Next;
- }
- CurrentObject = CurrentObject->Next;
- }
- }
-
-
- /*----------------------------------------------------------------------*/
-
-
- void TransformObject (ObjectCell *Object)
- /* This routine transforms each vertex of the object from object coords.
- to world coordinates */
-
- {
- VertexCell *CurrentVert;
- double tempHom[4];
-
- CurrentVert = Object->VertexHead;
-
- while (!(CurrentVert == NULL))
- {
- pointMultiply (CurrentVert->WorldPosition,Object->Transform.mat,tempHom);
- CurrentVert->WorldPosition.x = tempHom[0];
- CurrentVert->WorldPosition.y = tempHom[1];
- CurrentVert->WorldPosition.z = tempHom[2];
- CurrentVert = CurrentVert->Next;
- }
- }
-
-
- /***********************************************************************/
-
-
-